//
//  BaseTableViewCell.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2019/8/7.
//  Copyright © 2019年 Lssyyy. All rights reserved.
//

#import "BaseTableViewCell.h"
#define SepertorLineWidth       0.5f

@interface BaseTableViewCell ()

@property (nonatomic, strong) CAShapeLayer *sepertorLine;

@end
@implementation BaseTableViewCell

+ (instancetype)dequeueCellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
    [tableView registerClass:[self class] forCellReuseIdentifier:NSStringFromClass(self)];
    
    if (!indexPath) {
        return [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(self)];
    }
    
    return [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(self)
                                           forIndexPath:indexPath];
}

+ (instancetype)dequeueNibCellForTableView:(UITableView *)tableView forIndexPath:(NSIndexPath *)indexPath {
    NSString *className = NSStringFromClass([self class]);
    [tableView registerNib:[UINib nibWithNibName:className bundle:nil] forCellReuseIdentifier:className];
    if (!indexPath) {
        return [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(self)];
    }
    return [tableView dequeueReusableCellWithIdentifier:className forIndexPath:indexPath];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.showSepertorLine = NO;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self addUI];
        [self layoutUI];
    }
    return self;
}

- (void)addUI {
    if (self.showSepertorLine) {
        [self.layer addSublayer:self.sepertorLine];
    }
}

- (void)layoutUI {
    if (self.showSepertorLine) {
        [self.sepertorLine setPath:[self getSepertorPath].CGPath];
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (self.showSepertorLine) {
        [self.sepertorLine setPath:[self getSepertorPath].CGPath];
    }
    
}

#pragma mark - Setter Getter

- (void)setShowSepertorLine:(BOOL)showSepertorLine {
    _showSepertorLine = showSepertorLine;
    
    if (!showSepertorLine) {
        [self.sepertorLine removeFromSuperlayer];
    }
    else {
        [self.layer addSublayer:self.sepertorLine];
        [self.sepertorLine setPath:[self getSepertorPath].CGPath];
    }
    
    [self setNeedsLayout];
}

#pragma mark - Private Method

-(UIBezierPath *)getSepertorPath
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, self.height - SepertorLineWidth)];
    [path addLineToPoint:CGPointMake(self.width, self.height - SepertorLineWidth)];
    return path;
}

#pragma mark - Lazy Load


-(CAShapeLayer *)sepertorLine
{
    if (!_sepertorLine) {
        _sepertorLine = [CAShapeLayer layer];
        _sepertorLine.strokeColor = UIColorWithRGB16Radix(0xF1F1F1).CGColor;
        _sepertorLine.lineWidth = SepertorLineWidth;
    }
    return _sepertorLine;
}

@end
